<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\request\web;

#use \OutOfRangeException;
#use tehframework\request\Request;
#use tehframework\request\RequestCookiesHolder;
#use tehframework\request\RequestHeadersHolder;
#use tehframework\request\RequestFilesHolder;
#use tehframework\request\UploadedFile;

class WebRequest extends Request
	implements RequestCookiesHolder, RequestHeadersHolder, RequestFilesHolder
{
	/**
	 * @var array
	 */
	private $cookies = array();
	
	/**
	 * @var array
	 */
	private $headers = array();
	
	/**
	 * @var array
	 */
	private $files = array();
	
	/**
	 * @param  Request::METHOD_*? $method
	 * @param  array<string,mixed> $parameters
	 * @throws InvalidArgumentException
	 */
	public function __construct($method = null, array $parameters = array())
	{
		$this
			->setRequestMethod($method ? $method : self::METHOD_READ)
			->setParameters($parameters);
	}
	
	/**
	 * @param  Request $request
	 * @return WebRequest
	 */
	public function merge(Request $request)
	{
		if ($request instanceof RequestCookiesHolder)
		{
			$this->mergeCookies($request);
		}
		
		if ($request instanceof RequestHeadersHolder)
		{
			$this->mergeHeaders($request);
		}
		
		if ($request instanceof RequestFilesHolder)
		{
			$this->mergeFiles($request);
		}
		
		return $this->mergeParameters($request);
	}
	
	/**
	 * @param  string $name
	 * @return bool
	 */
	public function hasCookie($name)
	{
		return array_key_exists($name, $this->cookies);
	}
	
	/**
	 * @param  string $name
	 * @return string
	 * @throws OutOfRangeException
	 */
	public function getCookie($name)
	{
		if (!array_key_exists($name, $this->cookies))
		{
			throw new OutOfRangeException(sprintf(
				'Can not return [%s] cookie, because it does not exist.', $name
			));
		}
		
		return $this->cookies[$name];
	}
	
	/**
	 * @return array<string,string>
	 */
	public function getCookies()
	{
		return $this->cookies;
	}
	
	/**
	 * @param  string $name
	 * @param  string $value
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function setCookie($name, $value)
	{
		$this->assertIsLocked();
		
		$this->cookies[$name] = $value;
		
		return $this;
	}
	
	/**
	 * @param  array<string,string> $cookies
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function setCookies(array $cookies)
	{
		$this->assertIsLocked();
		
		$this->cookies = $cookies;
		
		return $this;
	}
	
	/**
	 * @param  string $name
	 * @return WebRequest
	 * @throws OutOfRangeException
	 * @throws BadMethodCallException
	 */
	public function removeCookie($name)
	{
		$this->assertIsLocked();
		
		if (!array_key_exists($name, $this->cookies))
		{
			throw new OutOfRangeException(sprintf(
				'Can not remove [%s] cookie, because it does not exist.', $name
			));
		}
		
		unset($this->cookies[$name]);
		
		return $this;
	}
	
	/**
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function clearCookies()
	{
		$this->assertIsLocked();
		
		$this->cookies = array();
		
		return $this;
	}
	
	/**
	 * @param  RequestCookiesHolder $cookiesHolder
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function mergeCookies(RequestCookiesHolder $cookiesHolder)
	{
		$this->assertIsLocked();
		
		$this->cookies =
			array_merge($this->cookies, $cookiesHolder->getCookies());
		
		return $this;
	}
	
	/**
	 * @param  string $name
	 * @return bool
	 */
	public function hasHeader($name)
	{
		return array_key_exists($name, $this->headers);
	}
	
	/**
	 * @param  string $name
	 * @return string
	 * @throws OutOfRangeException
	 */
	public function getHeader($name)
	{
		if (!array_key_exists($name, $this->headers))
		{
			throw new OutOfRangeException(sprintf(
				'Can not return [%s] header, because it does not exist.', $name
			));
		}
		
		return $this->headers[$name];
	}
	
	/**
	 * @return array<string,string>
	 */
	public function getHeaders()
	{
		return $this->headers;
	}
	
	/**
	 * @param  string $name
	 * @param  string $value
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function setHeader($name, $value)
	{
		$this->assertIsLocked();
		
		$this->headers[$name] = $value;
		
		return $this;
	}
	
	/**
	 * @param  array<string,string> $headers
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function setHeaders(array $headers)
	{
		$this->assertIsLocked();
		
		$this->headers = $headers;
		
		return $this;
	}
	
	/**
	 * @param  string $name
	 * @return WebRequest
	 * @throws OutOfRangeException
	 * @throws BadMethodCallException
	 */
	public function removeHeader($name)
	{
		$this->assertIsLocked();
		
		if (!array_key_exists($name, $this->headers))
		{
			throw new OutOfRangeException(sprintf(
				'Can not remove [%s] header, because it does not exist.', $name
			));
		}
		
		unset($this->headers[$name]);
		
		return $this;
	}
	
	/**
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function clearHeaders()
	{
		$this->assertIsLocked();
		
		$this->headers = array();
		
		return $this;
	}
	
	/**
	 * @param  RequestHeadersHolder $headersHolder
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function mergeHeaders(RequestHeadersHolder $headersHolder)
	{
		$this->assertIsLocked();
		
		$this->headers =
			array_merge($this->headers, $headersHolder->getHeaders());
		
		return $this;
	}
	
	/**
	 * @param  string $name
	 * @return bool
	 */
	public function hasFile($name)
	{
		return array_key_exists($name, $this->files);
	}
	
	/**
	 * @param  string $name
	 * @return UploadedFile
	 * @throws OutOfRangeException
	 */
	public function getFile($name)
	{
		if (!array_key_exists($name, $this->files))
		{
			throw new OutOfRangeException(sprintf(
				'Can not return [%s] file, because it does not exist.', $name
			));
		}
		
		return $this->files[$name][0];
	}
	
	/**
	 * @param  string $name
	 * @return UploadedFile[]
	 * @throws OutOfRangeException
	 */
	public function getFileArray($name)
	{
		if (!array_key_exists($name, $this->files))
		{
			throw new OutOfRangeException(sprintf(
				'Can not return [%s] file, because it does not exist.', $name
			));
		}
		
		return $this->files[$name];
	}
	
	/**
	 * @return array<string,UploadedFile[]>
	 */
	public function getFiles()
	{
		return $this->files;
	}
	
	/**
	 * @param  string $name
	 * @param  UploadedFile[] $value
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function setFile($name, array $value)
	{
		$this->assertIsLocked();
		
		$this->files[$name] = $value;
		
		return $this;
	}
	
	/**
	 * @param  array<string,UploadedFile[]> $files
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function setFiles(array $files)
	{
		$this->assertIsLocked();
		
		$this->files = $files;
		
		return $this;
	}
	
	/**
	 * @param  string $name
	 * @return WebRequest
	 * @throws OutOfRangeException
	 * @throws BadMethodCallException
	 */
	public function removeFile($name)
	{
		$this->assertIsLocked();
		
		if (!array_key_exists($name, $this->files))
		{
			throw new OutOfRangeException(sprintf(
				'Can not remove [%s] file, because it does not exist.', $name
			));
		}
		
		unset($this->files[$name]);
		
		return $this;
	}
	
	/**
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function clearFiles()
	{
		$this->assertIsLocked();
		
		$this->files = array();
		
		return $this;
	}
	
	/**
	 * @param  RequestFilesHolder $filesHolder
	 * @return WebRequest
	 * @throws BadMethodCallException
	 */
	public function mergeFiles(RequestFilesHolder $filesHolder)
	{
		$this->assertIsLocked();
		
		$this->files =
			array_merge($this->files, $filesHolder->getFiles());
		
		return $this;
	}
}